package cn.egame.extraction.service;

import java.rmi.RemoteException;
import java.util.List;

import org.apache.log4j.Logger;

import cn.egame.client.EGameClientExtV2;
import cn.egame.client.EGameClientV2;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.extraction.dao.UserGameDao;
import cn.egame.interfaces.EnumType.GameType;
import cn.egame.interfaces.gc.GameInfo;

public class UserGameService {
    private static byte[] lock = new byte[0];
    private static UserGameService instance = null;
    private Logger log = Logger.getLogger(GameStatusService.class);

    public static UserGameService getInstance() throws ExceptionCommonBase {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new UserGameService();
                }
            }
        }
        return instance;
    }

    private UserGameDao dao = null;

    public UserGameService() throws ExceptionCommonBase {
        dao = new UserGameDao();
    }

    public void deleteUserGameMore30() throws RemoteException{
    	dao.delUserGameByGId();
        List<Long> uIds = dao.listUId();
        for(long uId :uIds){
//            List<Integer> gIds =dao.listGIdByUId(uId);
//			for (int gId : gIds) {
//				GameInfo gameInfo = EGameClientV2.getInstance()
//						.getGameInfoById(0, 0, gId);
//				if (gameInfo != null
//						&& gameInfo.getGameType().value() == GameType.app
//								.value()) {
//					dao.delUserGameByGIdAndUId(gId, uId);
//				}

//			}
        	List<Integer> gIds =dao.listGIdByUId(uId);
			if (gIds != null && gIds.size() > 12) {
				gIds = gIds.subList(12, gIds.size());
//				for (int gId : gIds) {
//					dao.delUserGameByGIdAndUId(gId, uId);
//				}
				dao.delUserGameByGIdSAndUId(gIds, uId);
				
			}
            EGameClientExtV2.getInstance().reloadlistUserGameGIdByUId(uId);
        }
    }
    public static void main(String[] args) throws RemoteException {
        UserGameService.getInstance().deleteUserGameMore30();
    }
    
}
